package com.open.capacity.task.aspect;

import com.open.capacity.common.model.SysTask;
import com.open.capacity.task.dao.TaskDao;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class TaskAspect {
    @Autowired
    private TaskDao taskDao;

    @Pointcut("execution(* com.open.capacity.task.service.impl.TaskServiceImpl.*(..))")
    private void pointcut() {
    }

    @Before("pointcut()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //        修改状态
        List<SysTask> tasks = taskDao.findAll();
        for (SysTask sysTask : tasks) {
            try {
                if (sdf.parse(sysTask.getEndtim()).before(new Date())) {
                    taskDao.endTaskByTaskid(sysTask.getTaskid());
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
    }
}
